package engine.motionDetection.modelisation;

import java.awt.image.BufferedImage;
import java.util.Arrays;

import engine.utils.GeneralUtils;

public class BooleanImage extends ImagePix<Boolean>{
	private BooleanImage(BufferedImage backgroundImage,
			BufferedImage foregroundImage, int threshold){
		super(backgroundImage.getWidth(), backgroundImage.getHeight());
		System.out.println("engine.motionDetection.modelisation : BooleanImage");
		int width =-1 ,height = -1;
		if(backgroundImage.getWidth() == foregroundImage.getWidth()
				&& backgroundImage.getHeight() 
									== backgroundImage.getHeight()){
			width = backgroundImage.getWidth();
			height = foregroundImage.getHeight();
		}else{
			throw new IllegalArgumentException("the two image should " +
					"have the same size");
		}
		for(int j = 0; j<height; j++){
			for(int i = 0; i<width; i++){
				int bgGray = 
					GeneralUtils.fromRGB2Gray(
							backgroundImage.getRGB(i, j));
				int fgGray = 
					GeneralUtils.fromRGB2Gray(
							foregroundImage.getRGB(i, j));
				int substractedGray = Math.abs(bgGray - fgGray);
				//now threshold
				if(substractedGray > threshold){
					this.setPix(i, j, true);
				}else{
					this.setPix(i, j, false);
				}
			}
		}
	}
	public BooleanImage(int width, int height){
		super(width, height, new Boolean(false));
	}
	
	
	public BooleanImage(BooleanImage boolImg) {
		super(boolImg);
	}
	public static BooleanImage CreateBooleanImageFromBufferedImages(
			BufferedImage background, BufferedImage foreground,
			int threshold){
		System.out.println("engine.motionDetection.modelisation : CreateBooleanImageFromBufferedImages");
		return new BooleanImage(background, foreground, threshold);
	}
}
